export default {
  namespaced: true,
  state: {
    type: "all",
    list: JSON.parse(localStorage.getItem("tototo")) || [],
    // list: [
    //   {
    //     id: 100,
    //     name: "吃饭",
    //     isDone: true,
    //   },
    //   {
    //     id: 201,
    //     name: "睡觉",
    //     isDone: false,
    //   },
    //   {
    //     id: 103,
    //     name: "打豆豆",
    //     isDone: true,
    //   },
    // ],
  },
  mutations: {
    changeFn(state, id) {
      const obj = state.list.find((item) => item.id === id);
      obj.isDone = !obj.isDone;
    },
    delFn(state, id) {
      state.list = state.list.filter((item) => item.id !== id);
    },
    add(state, content) {
      state.list.unshift({
        id: +new Date(),
        name: content,
        isDone: false,
      });
    },
    changeIsAll(state, val) {
      state.list.forEach((item) => (item.isDone = val));
    },
    clearFnn(state) {
      state.list = state.list.filter((item) => !item.isDone);
    },
    changeType(state, val) {
      state.type = val;
    },
  },
  actions: {
    clearFn(context) {
      setTimeout(() => {
        context.commit("clearFnn");
      }, 1000);
    },
  },
  getters: {
    leftCount(state) {
      return state.list.filter((item) => !item.isDone).length;
    },
    showFn(state) {
      return state.list.some((item) => item.isDone);
    },
    showList(state) {
      if (state.type === "active") {
        return state.list.filter((item) => !item.isDone);
      } else if (state.type === "complete") {
        return state.list.filter((item) => item.isDone);
      } else {
        return state.list;
      }
    },
  },
};
